package com.example.androidsx;

import android.accounts.Account;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class zhuceye extends AppCompatActivity {
    private SQLcj dbHelper;
    private EditText id,username,usereml,userpwd;
    private ImageView captchaImage;
    private int[] captchaDrawables = {R.drawable.yan1, R.drawable.yan2, R.drawable.yan3};
    private int currentIndex = 0;
    Button zcbt, regitst  ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuceye);
        dbHelper = new SQLcj(this);
        captchaImage = findViewById(R.id.captcha_image);
        zcbt = findViewById(R.id.zcbt_dl);
        regitst= findViewById(R.id.register_button);
        id = findViewById(R.id.userid);
        username = findViewById(R.id.username);
        usereml = findViewById(R.id.email);
        userpwd = findViewById(R.id.password);



        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });






        regitst.setOnClickListener(v -> addAccount());

        captchaImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = (currentIndex + 1) % captchaDrawables.length;
                captchaImage.setImageResource(captchaDrawables[currentIndex]);
            }
        });


        zcbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
    }

    private void addAccount(){
        String userid=id.getText().toString().trim();
        String Username=username.getText().toString().trim();
        String Userpwd= userpwd.getText().toString().trim();
        String Usereml=usereml.getText().toString().trim();

        if (Username.isEmpty() || Userpwd.isEmpty()) {
            if (Username.isEmpty()) {
                username.setError("用户名不能为空");
            }
            if (Userpwd.isEmpty()) {
                userpwd.setError("密码不能为空");
            }
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        if (dbHelper.isUsernameExists(userid,Username)) {
            if(userid.isEmpty()){
                username.setError("普通用户名已存在");
                Toast.makeText(this, "普通用户名已存在，请使用其他用户名", Toast.LENGTH_SHORT).show();
                return;
            }else {
                username.setError("管理员用户名已存在");
                Toast.makeText(this, "管理员用户名已存在，请使用其他用户名", Toast.LENGTH_SHORT).show();
                return;
            }

             }
    long id= dbHelper.addAccount(userid,Username,Userpwd,Usereml);
    if(id!=-1){

        if (userid.isEmpty()){
            Toast.makeText(this,"账号添加成功,普通账号。",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"账号添加成功,管理员账号",Toast.LENGTH_SHORT).show();
        }
      clearInputs();
    }else {
        Toast.makeText(this,"添加失败",Toast.LENGTH_SHORT).show();
    }
    }
  private void clearInputs(){
        id.setText("");
        username.setText("");
        userpwd.setText("");
        usereml.setText("");
  }


}